package com.example.stus.contorller;

import com.example.stus.entity.StuInformation;
import com.example.stus.resposistory.StuInformationReposistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/StuInformation")
public class StuInformationController {
    @Autowired
    private StuInformationReposistory s;
    //分页接口，返回页数
    @GetMapping("/findAll/{page}/{size}")
    public Page<StuInformation> findAll(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        return s.findAll(pageable);
    }
    //添加操作
    @PostMapping("/save")
    public String save(@RequestBody StuInformation stu){
        StuInformation result =  s.save(stu);
        if ( result!=null )
        {
            return "success";
        }
        else{
            return "error";
        }
    }
    //从前端接收要修改的Id（sno）
    @GetMapping("/findBySno/{sno}")
    public StuInformation findBySno(@PathVariable("sno") String sno){
        return s.findBySno(sno);
    }

    @PutMapping("/update")
    public String update(@RequestBody StuInformation stuInformation){
        StuInformation result = s.save(stuInformation);
            if ( result != null )
            {
                return "success";
            } else{
                return "error";
            }
    }



    @DeleteMapping("/deleteById/{sno}")
    public void deleteById(@PathVariable("sno") String sno){
        s.deleteById(sno);
    }

//    @DeleteMapping("/doData/{sno}")
//    public void doData(@PathVariable("sno") String sno){
//        s.doData(sno);
//    }
}
